package com.qzxl.conntext.utils;

/**
 * @Auther: jintianyun
 * @Date: 2020/9/23 13:53
 * @Description:
 */
public class CRC32 {
    private int crc;
    private String mx;

    public CRC32(String mx) {
        crc = 0x00000000;
        this.mx = mx;
        getCrc();
    }

    private void crc(byte data) {
        int point = 0b10000000;
        while (point > 0) {
            if ((((data & point) == point) ^ ((crc & 0x80000000) == 0x80000000))) {
                crc = (crc << 1) ^ 0x04c11d87;
            } else {
                crc <<= 1;
            }
            point >>= 1;
        }
    }

    private void getCrc() {
        byte[] bytes = mx.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            crc(bytes[i]);
        }
    }

    public String getCRC() {
        return Integer.toHexString(crc);
    }

}
